<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        //【1】in操作符的直接使用
        function Person() {}
        Person.prototype.name = "Nicholas";
        Person.prototype.age = 29;
        Person.prototype.job = "Software Engineer";
        Person.prototype.sayName = function() {
            console.log(this.name);
        };
        var person1 = new Person();
        var person2 = new Person();
        console.log(person1.hasOwnProperty("name")); //false 
        console.log("name" in person1); //true 
        person1.name = "Greg";
        console.log(person1.name); //"Greg" ——来自实例
        console.log(person1.hasOwnProperty("name")); //true 
        console.log("name" in person1); //true 
        console.log(person2.name); //"Nicholas" ——来自原型
        console.log(person2.hasOwnProperty("name")); //false 
        console.log("name" in person2); //true 
        delete person1.name;
        console.log(person1.name); //"Nicholas" ——来自原型
        console.log(person1.hasOwnProperty("name")); //false 
        console.log("name" in person1); //true
        //【1-1】只抓取原型上的属性
        //封装函数
        function hasPrototypeProperty(object, name) {
            return !object.hasOwnProperty(name) && (name in object);
        }
        //使用函数
        function Person() {}
        Person.prototype.name = "Nicholas";
        Person.prototype.age = 29;
        Person.prototype.job = "Software Engineer";
        Person.prototype.sayName = function() {
            console.log(this.name);
        };
        var person = new Person();
        console.log(hasPrototypeProperty(person, "name")); //true 
        person.name = "Greg";
        console.log(hasPrototypeProperty(person, "name")); //false
        //【2】Object.keys()方法抓取所有可枚举实例属性
        function Person() {}
        Person.prototype.name = "Nicholas";
        Person.prototype.age = 29;
        Person.prototype.job = "Software Engineer";
        Person.prototype.sayName = function() {
            console.log(this.name);
        };
        var keys = Object.keys(Person.prototype);
        console.log(keys); //["name", "age", "job", "sayName"]
        var p1 = new Person();
        p1.name = "Rob";
        p1.age = 31;
        var p1keys = Object.keys(p1);
        console.log(p1keys); //["name", "age"]
        //【2-1】Object.getOwnPropertyNames(); 如果你想要得到所有实例属性，无论它是否可枚举
        var keys = Object.getOwnPropertyNames(Person.prototype);
        console.log(keys); // ["constructor", "name", "age", "job", "sayName"]【注意此处有constructor属性】
    </script>
</body>

</html>